package article

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"sola/common"
	"sola/db"
	"sola/router/middleware"
	"sola/utils"
)

func create(c *gin.Context) {
	var article db.Article
	if err := c.ShouldBind(&article); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	user, err := middleware.GetUser(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	article.AuthorId = user.UserId
	article.AuthorName = user.Username
	article.AuthorNickName = user.Nickname
	article.UpdateUserId = user.UserId
	article.UpdateUserName = user.Username
	article.UpdateUserNickName = user.Nickname
	category, err := db.FindByCategoryId(article.CategoryId)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	article.CategoryName = category.CategoryName
	article.ClassifyId = category.ClassifyId
	article.ClassifyName = category.ClassifyName
	article.UpdateUserId = user.UserId
	article.UpdateUserName = user.Username
	article.UpdateUserNickName = user.Nickname
	db.Creaete(&article)
	utils.ResponseSuccess(c, &article)
	return
}

func update(c *gin.Context) {
	var article db.Article
	if err := c.ShouldBind(&article); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	var articleUriInfo db.Article
	if err := c.ShouldBindUri(&articleUriInfo); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if article.ArticleId != "" && article.ArticleId != articleUriInfo.ArticleId {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	user, err := middleware.GetUser(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	article.UpdateUserId = user.UserId
	article.UpdateUserName = user.Username
	article.UpdateUserNickName = user.Nickname
	if article.CategoryId != "" {
		category, err := db.FindByCategoryId(article.CategoryId)
		if err != nil {
			utils.ResponseError(c, utils.ErrCodeParameter)
			return
		}
		article.CategoryName = category.CategoryName
		article.ClassifyId = category.ClassifyId
		article.ClassifyName = category.ClassifyName
	}
	db.Update(&article)
	utils.ResponseSuccess(c, &article)
	return
}

func list(c *gin.Context) {
	var baseSearchModel common.BaseSearchModel
	if err := c.ShouldBind(&baseSearchModel); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if err := c.ShouldBindUri(&baseSearchModel); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	if baseSearchModel.UserId == "" {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	data := make(map[string]interface{})
	count := db.CountArticle(&baseSearchModel)
	articles := db.GetArticleList(&baseSearchModel)
	data["total"] = count
	data["items"] = articles
	utils.ResponseSuccess(c, data)
	return
}

func detail(c *gin.Context) {
	var articleInfo db.Article
	if err := c.ShouldBindUri(&articleInfo); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	article, err := db.FindByArticleId(articleInfo.ArticleId)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	utils.ResponseSuccess(c, article)
	return
}

func delete(c *gin.Context) {
	var articleInfo db.Article
	if err := c.ShouldBindUri(&articleInfo); err != nil {
		fmt.Println("ShouldBind", err)
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	article, err := db.FindByArticleId(articleInfo.ArticleId)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeParameter)
		return
	}
	user, err := middleware.GetUser(c)
	if err != nil {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	if article.AuthorId != user.UserId {
		utils.ResponseError(c, utils.ErrCodeUserNotExist)
		return
	}
	article.UpdateUserId = user.UserId
	article.UpdateUserName = user.Username
	article.UpdateUserNickName = user.Nickname
	db.Delete(&article)
	utils.ResponseSuccess(c, &article)
	return
}
